package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;
    @RequestMapping(value = "/depts",method = RequestMethod.GET)
    public Result list(){
        log.info("查询全部的部门数据");
       // System.out.println("查询全部的部门数据");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);

    }
    @DeleteMapping(value = "/depts")
    //方法形参名字与请求参数一致时,可以省略@RequestParam注解
    public Result delete(@RequestParam("id") Integer deptid){
        //System.out.println("删除id为"+deptid+"的部门数据");
        log.info("删除id为"+deptid+"的部门数据");
        deptService.deleteById(deptid);
        return Result.success();
    }
    @PostMapping (value = "/depts")
    public Result add(@RequestBody Dept dept){
        //System.out.println("新增部门"+dept);
        log.info("新增部门"+dept);
        deptService.add(dept);
        return Result.success();
    }
    @GetMapping (value = "/depts/{id}")
    public Result getInfo(@PathVariable Integer id){
        //System.out.println("查询id为"+id+"的部门数据");
        log.info("查询id为"+id+"的部门数据");
        Dept dept = deptService.findById(id);
        return Result.success(dept);
    }
    @PutMapping (value = "/depts")
    public Result update(@RequestBody Dept dept){
        //System.out.println("修改部门"+dept);
        log.info("修改部门"+dept);
        deptService.update(dept);
        return Result.success();
    }
}
